package cn.edu.ctbu.kcsj.service.impl;

import cn.edu.ctbu.kcsj.domain.Course;
import cn.edu.ctbu.kcsj.repository.CourseRepository;
import cn.edu.ctbu.kcsj.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseImplService implements CourseService {
    @Autowired
    private CourseRepository courseRepository;
    public Course getByTno(Integer tno) {
        Course course = courseRepository.findByTno(tno);
        return course;
    }
    public  List<Course> findAll() {
        return courseRepository.findAll();
    }
    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Course> findAll(Pageable pageable){
        return courseRepository.findAll(pageable);
    }
    /**
     * 分页，带过滤的查询
     * @param course
     * @param pageable
     * @return
     */
    public Page<Course> findAll(Example<Course> course, Pageable pageable){
        return courseRepository.findAll(course,pageable);
    }
    public Course insert(Course course){

        return courseRepository.save(course);
    }
    public Course update(Course course){
        return courseRepository.save(course);
    }
    public void delete(Course course){

        courseRepository.delete(course);
    }
    public void delete(Integer cid){
        Course course=new Course();
        course.setCid(cid);
        courseRepository.delete(course);
    }
    public Course getByCid(Integer cid){
        Course course=courseRepository.findById(cid).orElse(null);
        return course;
    }

}
